﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
 
namespace Launcher
{
    public partial class newitem : Form
    {
        private Form1 frm1;
        public newitem(Form1 form)
        {
            InitializeComponent();
            frm1 = form;
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Exe names (*.exe) |*.exe| All files | *.*";
            openFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();
            openFileDialog1.Title = "Select a file to add to the launcher list";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            openFileDialog1.AddExtension = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                textBox1.Text = openFileDialog1.FileName.ToString();
        }
        
        private void button2_Click(object sender, EventArgs e)
        {

            if (textBox1.Text != "" && textBox1.Text != "")
            {
                string dir = Directory.GetCurrentDirectory();
                string path = dir + @"\\items.xml";
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(fs);
                if (textBox1.Text.Contains(".exe"))
                {
                    XmlElement xe = xmldoc.CreateElement("Item");
                    XmlAttribute xa = xmldoc.CreateAttribute("name");
                    XmlAttribute type = xmldoc.CreateAttribute("imagekey");
                    type.Value = "0";
                    xa.Value = textBox2.Text;
                    xe.InnerText = textBox1.Text;
                    xe.SetAttributeNode(xa);
                    xe.SetAttributeNode(type);
                    //xmldoc.DocumentElement.InsertAfter(xe, xmldoc.DocumentElement.FirstChild);
                    xmldoc.DocumentElement.FirstChild.AppendChild(xe);
                    FileStream fsxml = new FileStream(path,FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
                    xmldoc.Save(fsxml);
                    ListViewItem lvi = new ListViewItem(textBox2.Text);
                    lvi.Tag = textBox1.Text;
                    lvi.ImageIndex = (int)Int64.Parse("0");
                    
                    lvi.SubItems.Add(textBox1.Text);
                    frm1.listView1.Items.Add(lvi);
                    
                    fsxml.Close();
                    fsxml.Dispose();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("File must be an application!...For now");
                }
            }
            else
            {
                MessageBox.Show("You must provide a title and a path to a valid application!");
            }
        }

        private void newitem_Load(object sender, EventArgs e)
        {

        }
    }
}
